package com.handyshop.mall.user.controller;

import cn.hutool.core.bean.BeanUtil;
import com.handyshop.mall.basic.core.vo.ResultVo;
import com.handyshop.mall.dubbo.user.dto.platform.SysPlatformAddDto;
import com.handyshop.mall.dubbo.user.dto.platform.SysPlatformEditDto;
import com.handyshop.mall.dubbo.user.entity.SysPlatform;
import com.handyshop.mall.user.service.SysPlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hs
 * @date 2020/11/19
 */
@RestController
@RequestMapping("/sysPlatform")
@RequiredArgsConstructor
@Api(tags = "系统平台")
public class SysPlatformController {
    private final SysPlatformService sysPlatformService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询平台和平台菜单")
    public ResultVo<SysPlatform> getById(@PathVariable Long id) {
        return ResultVo.success(sysPlatformService.findById(id));
    }

    @PostMapping
    @ApiOperation(value = "添加")
    //@PreAuthorize("@pms.hasPermission('sys_platform_add')")
    public ResultVo<Boolean> save(@Validated @RequestBody SysPlatformAddDto sysPlatformAddDto) {
        SysPlatform sysPlatform = BeanUtil.copyProperties(sysPlatformAddDto, SysPlatform.class);
        return ResultVo.success(sysPlatformService.save(sysPlatform));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    //@PreAuthorize("@pms.hasPermission('sys_platform_del')")
    public ResultVo<Boolean> removeById(@PathVariable Long id) {
        return ResultVo.success(sysPlatformService.removeById(id));
    }

    @PutMapping
    @ApiOperation(value = "根据id编辑")
    //@PreAuthorize("@pms.hasPermission('sys_platform_edit')")
    public ResultVo<Boolean> update(@Validated @RequestBody SysPlatformEditDto sysPlatformEditDto) {
        SysPlatform sysPlatform = BeanUtil.copyProperties(sysPlatformEditDto, SysPlatform.class);
        return ResultVo.success(sysPlatformService.updateById(sysPlatform));
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "全部系统平台列表")
    public ResultVo<List<SysPlatform>> findAll() {
        return ResultVo.success(sysPlatformService.list());
    }

}